#include "server.h"
#include "game.h"
#include "unitemitter.h"
#include <QDebug>

UnitEmitter::UnitEmitter(const qint32 ID)
   : Entity(ID)
{
   mBaseStats.type = ENTITY_UNITEMITTER;
}

void UnitEmitter::tick(const qreal timeDelta)
{
}

void UnitEmitter::think()
{
   //qDebug()<< "UnitEmitter::think() id" << id();

   if (!mQueue.isEmpty())
   {
      EntitySquadType &squad = mQueue.front();
      //qDebug()<< "UnitEmitter::think() squad id=" << squad.id << "size=" << squad.size
      //<< "type=" << squad.stats.type << "kind=" << squad.stats.kind;

      Unit *unit = new Unit();
      unit->setBaseStats(squad.stats);
      unit->setPosition(mEmitPosition);
      unit->setSquadID(squad.id);
      unit->setPlayerID(squad.playerID);
      unit->path = mGame->path(mPathName);

      mGame->addEntity(unit);    // will assign ent id
      mGame->server->sendEntityAdd(unit);

      squad.size--;
      if (squad.size == 0)
      {
         mGame->server->sendEntitySquadRemoveFromEmitter(id(), squad.id);
         mQueue.dequeue();
      }
   }
   setNextThinkTime(mGame->currentTime().addMSecs(1000));
}


void UnitEmitter::marshall(const EVENT_TYPE event, QDataStream &data)
{
   switch (event)
   {
   case EVENT_ENTITY_ADD:
      Entity::marshall(event, data);
      data << (quint32)mCapacity
           << (qreal)mEmitRate
           << (qreal)mEmitPosition.x()
           << (qreal)mEmitPosition.y()
           << (QString)mPathName;
      qDebug() << "UnitEmitter::marshall cap=" << mCapacity << "rate=" << mEmitRate << "pos=" << mEmitPosition << "path=" << mPathName;
      break;
   default:
      break;
   }
}


void UnitEmitter::demarshall(const EVENT_TYPE event, QDataStream &data, const quint32 playerID)
{
   switch (event)
   {
   case EVENT_ENTITY_SQUAD_ADD_TO_EMITTER:
   {
      qint32 type=0;
      EntitySquadType squad;
      squad.isDeployed = false;
      squad.playerID = playerID;
      // Demarshall squad info
      data >> squad.id >> squad.size >> type >> squad.stats.kind >> squad.stats.size >> squad.stats.color;
      squad.stats.type = (ENTITY_TYPE)type;
      if ((quint32)mQueue.size() < mCapacity)
         enqueueSquad(squad);
      break;
   }
   default:
      break;
   }
}


void UnitEmitter::enqueueSquad(const EntitySquadType &squad)
{
   mQueue.enqueue(squad);
   mGame->entityManager.addEntitySquad(squad);
   mGame->server->sendEntitySquadAddToEmitter(id(), squad);
}

QPointF UnitEmitter::emitPosition() { return mEmitPosition; }
void UnitEmitter::setEmitPosition(const QPointF &position) { mEmitPosition = position; }

quint32 UnitEmitter::capacity() { return mCapacity; }
void UnitEmitter::setCapacity(const quint32 capacity) { mCapacity = capacity; }

qreal UnitEmitter::rate() { return mEmitRate; }
void UnitEmitter::setRate(const qreal rate) { mEmitRate = rate; }

QString UnitEmitter::pathName() { return mPathName; }
void UnitEmitter::setPathName(const QString &path) { mPathName = path; }
